function getWebGLContext(id){
    var canvas = document.getElementById(id);
    var gl = canvas.getContext("webgl");
    if(!gl){
        console.log("Unable to initialize WebGL. Your browser may not support it.");
    }
    return gl;
}

function buildProgram(gl,vertexSource,fragmentSource){
    var vertexShader = createShader(gl,gl.VERTEX_SHADER,vertexSource)
    var fragmentShader = createShader(gl,gl.FRAGMENT_SHADER,fragmentSource)
    var program = gl.createProgram()
    // 绑定着色器对象
    gl.attachShader(program,vertexShader)
    gl.attachShader(program,fragmentShader)
    // 链接程序
    gl.linkProgram(program)
    if(!gl.getProgramParameter(program,gl.LINK_STATUS)){
        console.log('无法链接程序，错误信息为： ' + gl.getProgramInfoLog(program))
        throw new Error('无法链接程序')
    }
    // 使用程序
    gl.useProgram(program)

    return program
}

function  createShader(gl,type,source){
    var shader = gl.createShader(type)
    gl.shaderSource(shader,source)
    gl.compileShader(shader)
    if(!gl.getShaderParameter(shader,gl.COMPILE_STATUS)){
        console.log('无法编译着色器 , 错误信息为： ' + gl.getShaderInfoLog(shader))
        throw new Error('无法编译着色器')
    }
    return shader;
}
